import {
  StockNode,
  getStockNodeRpcDto,
  GateRiverStockGateway,
  putStockNodeRootRpcDto,
  copyStockNodeLinkRpcDto,
  GateRiverStockInterfaceWithDerive,
  PutRes,
} from "qqlx-core";

import { ViewBase } from "@view/view-base";

export class ViewGateRiverStock extends ViewBase<StockNode> implements GateRiverStockInterfaceWithDerive<{}> {
  private static instance: ViewGateRiverStock;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewGateRiverStock();
    }

    if (!this.instance) this.instance = new ViewGateRiverStock();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  async getNodeTree(dto: getStockNodeRpcDto): Promise<StockNode[][]> {
    const path = `${GateRiverStockGateway.path}/node/tree`;
    return this.$request.getByPut<getStockNodeRpcDto, StockNode[][]>(path, dto);
  }

  async copyLink(dto: copyStockNodeLinkRpcDto) {
    const path = `${GateRiverStockGateway.path}/node/link/copy`;
    return this.$request.patch<copyStockNodeLinkRpcDto, null>(path, dto);
  }

  async putRootSync(dto: putStockNodeRootRpcDto) {
    const path = `${GateRiverStockGateway.path}/node/root`;
    return this.$request.put<putStockNodeRootRpcDto, PutRes<StockNode>>(path, dto);
  }
}
